package g1.thermostat;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Chronometer;
import android.widget.Chronometer.OnChronometerTickListener;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.Button;
import java.util.HashMap;

public class T1Activity extends Activity {
    /** Called when the activity is first created. */
	
	Chronometer chrono;
//	TextView txt;
	TextView tvTemp;
	TextView tvDay;
	TextView tvTime;
	Button btn1;
	Button btn2;
	Button btn3;
	
	final String[] WEEKDAYS = {"monday", "tuesday" , "wednesday", "thursday", "friday", "saturday", "sunday"};
	
	double dayTemp;
	double nightTemp;
	double ovTemp;
	
	int dayState;
	
	int nextChDay;
	int nextDayState;
	int nextChTime;
	
	boolean changedBool = false;
	
	String displayTime;
//	long startTime=SystemClock.elapsedRealtime();
	boolean chronoStart=true;
	
	//boolean override = ((Variables)getApplication()).getOvEnable();
	
	@Override
	public void onResume(){
		super.onResume();
		dayTemp = ((Variables)this.getApplication()).getDayTemperature();
		ovTemp = ((Variables)this.getApplication()).getOvTemperature();
		
		if (!((Variables)this.getApplication()).getOvEnable()){
			tvTemp.setText(Double.toString(dayTemp));
		}else{
			tvTemp.setText(Double.toString(ovTemp));
		}
		
		changedBool = ((Variables)getApplication()).checkChangedSchedule();
	}
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	((Variables)getApplication()).createTimesList();
    	((Variables)getApplication()).addTimeInList("monday", "day", 130);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btn1=(Button)findViewById(R.id.btn1);
        btn2=(Button)findViewById(R.id.btn2);
        btn3=(Button)findViewById(R.id.btn3);
        tvDay=(TextView)findViewById(R.id.tvDay);
        tvTemp=(TextView)findViewById(R.id.tvTemp);
        tvTime=(TextView)findViewById(R.id.tvTime);
        dayState = 0;
        nextChDay = 0;
    	nextDayState = 0;
    	nextChTime = 0;
    	setState();
    	int nextChan = ((Variables)getApplication()).getNextChange(0);
    	Log.i("nextChan",""+nextChan);
    	setNewChange(nextChan);
    	
    	
        
//      dayTemp = ((Variables)this.getApplication()).getDayTemperature();
		
      	chrono = (Chronometer) findViewById(R.id.chrono);
      		
      	if(chronoStart){
      		chronoStart=false;
      		chrono.setBase(SystemClock.elapsedRealtime());
      		chrono.start();
      	}
		chrono.setOnChronometerTickListener(new OnChronometerTickListener() {

		@Override
		public void onChronometerTick(Chronometer arg0) {

			int seconds = (int) (((SystemClock.elapsedRealtime() - chrono.getBase()))/1000);
			int minF = seconds*5;
			int hourF = minF / 60;
			int  dayF = hourF / 24;
			int dayNum =dayF%7;
				
			int cTime = (hourF%24)*100 + (minF%60);
				
			if ((cTime == nextChTime && nextChDay == dayNum) || changedBool) {
				changedBool = false;
				Log.i("ChangeDet","Yes");
				if (dayState == 1) {
					dayState = 0;
				} else if (dayState == 0) {
					dayState = 1;
				}
				Log.i("cTime1",cTime+"");
				int currentTime = dayNum*100000 + dayState*10000 + (hourF%24)*100 + (minF%60);
				int nextCh = ((Variables)getApplication()).getNextChange(currentTime);
				setNewChange(nextCh);
				setState();
				Log.i("cTime2",cTime+"");
			}
			String mins = Integer.toString(minF % 60);
			String hours = Integer.toString(hourF % 24);
				
			//get the time of the next change and compare with the actual time
			//if the time is right then do the action and update with the next time change
			
			if (mins.length() == 1) {
				mins = "0"+mins;
			}
			if (hours.length() == 1){
				hours = "0"+hours;
			}
				
			displayTime = WEEKDAYS[dayNum] + "  " + hours + ":" + mins;
			arg0.setText(displayTime);	
			}

		});
        
        
        
        tvTemp.setText(Double.toString(dayTemp));
        
        btn1.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent=new Intent();
				intent.setClass(T1Activity.this, T2Activity.class);
				startActivity(intent);
				
			}
        	
        });
        btn2.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent=new Intent();
				intent.setClass(T1Activity.this, T3Activity.class);
				startActivity(intent);
				
			}
        	
        });
        btn3.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent=new Intent();
				intent.setClass(T1Activity.this, T4Activity.class);
				startActivity(intent);
				
			}
        	
        });
        
    }
    
    public void setNewChange(int nextCha) {
    	
		//remember set day/night
		if (nextCha > -1) {
			
			int dayOfWeek = nextCha/100000;
	    	int State = (nextCha / 10000)%10;
	    	int timer = nextCha % 10000;
	    
	    	nextChDay = dayOfWeek;
	    	nextDayState = State;
	    	nextChTime = timer;
	    	
	    	Log.i("nextChangeInfo",nextChDay+"");
	    	Log.i("nextChangeInfo",nextDayState+"");
	    	Log.i("nextChangeInfo",nextChTime+"");
	    	
	    	String hh = ""+timer/100;
	    	String mm = ""+timer%100;
	    	Log.d("text",hh+mm);
	    	
	    	if (hh.length() == 1 || hh.equals("0")) {
	    		hh = "0" + hh;
	    	}
	    	
	    	if (mm.length() <= 1 || mm.equals("0")) {
	    		mm = "0" + mm;
	    	}
	    	
	    	Log.d("text",hh+mm);
	    	
	    	tvTime.setText(WEEKDAYS[nextChDay]+" "+nextDayState + " "+ hh + ":" + mm);
	    	Log.i("nextCha", nextCha+"");

		} else {
			tvTime.setText("No Next Change Found");
		}

    }
    
    public void setState() {
    	Log.i("dayState",""+dayState);
    	if (dayState == 0) {
    		tvDay.setText("Night");
    	} else if (dayState == 1) {
    		tvDay.setText("Day");
    	}
    }
}